﻿#pragma strict
#pragma implicit
#pragma downcast

public var owner : NetworkPlayer;

var speed : float = 6;
var turnSpeed : float = 3;
var breakSpeed : float = 2;

var middleBackThrust : GameObject;
var leftBackThrust : GameObject;
var rightBackThrust : GameObject;
var leftFrontThrust : GameObject;
var rightFrontThrust : GameObject;

var shot : GameObject;
var shotting : GameObject;
var objectPosRot : GameObject;

//Last input value, we're saving this to save network messages/bandwidth.
private var lastClientCommandInput : int = 0;

function Start () {

}

function Update(){
	
	//Client code
	if(owner!=null && Network.player==owner){
		//Only the client that owns this object executes this code
		
		var CommandInput : int = GetInputNum();

		//Is our input different? Do we need to update the server?
		if(lastClientCommandInput != CommandInput) {
				lastClientCommandInput = CommandInput;			
				//Too bad a server can't send an rpc to itself using "RPCMode.Server"!...bugged :[
				SendMovementInput(CommandInput);
		}
		else if(Network.isClient){
			SendMovementInput(CommandInput); //Use this (and line 64) for simple "prediction"
			networkView.RPC("SendMovementInput", RPCMode.Server, CommandInput);
		}
	}			
	
	//Server movement code
	if(Network.isServer){//Also enable this on the client itself: "|| Network.player==owner){|"
		
		//Actually move the player using his/her input
		ServerMovement(lastClientCommandInput);
	}
	
	//Both Server and Client
	//play/stop particle and animations based on input
	AnimateInput(lastClientCommandInput);
}

function GetInputNum() :int {
	if (Input.GetKey(KeyCode.W)) {
		return 1;
	}		
	if (Input.GetKey(KeyCode.A)) {
		return 2;
	}
	if (Input.GetKey(KeyCode.D)) {
		return 3;
	}
	if (Input.GetKey(KeyCode.S)) {
		return 4;
	}
	if (Input.GetKeyDown(KeyCode.Space)) {
		return 5;
	}
	return 0;
}

function ServerMovement(CommandInput : int) {
	if (CommandInput == 1) {
		rigidbody.AddForce(speed*transform.forward);
	}
	if (CommandInput == 2) {
		transform.Rotate(0,-turnSpeed,0);	
	}
	if (CommandInput == 3) {
		transform.Rotate(0,turnSpeed,0);
	}		
	if (CommandInput == 4) {
		rigidbody.AddForce(-breakSpeed*transform.forward);
	}
	if (CommandInput == 5) {
		Shot();
	}
}

function AnimateInput(CommandInput : int) {
	if (CommandInput == 1) {
		Play(middleBackThrust);
	}
	else {
		Stop(middleBackThrust);
	}
	
	if (CommandInput == 2) {
		Play(rightBackThrust);
		Play(leftFrontThrust);
	}
	else {
		Stop(rightBackThrust);
		Stop(leftFrontThrust);
	}
	
	if (CommandInput == 3) {
		Play(leftBackThrust);
		Play(rightFrontThrust);
	}
	else {
		Stop(leftBackThrust);
		Stop(rightFrontThrust);
	}
		
	if (CommandInput == 4) {
		Play(leftFrontThrust);
		Play(rightFrontThrust);
	}
	else {
		Stop(leftFrontThrust);
		Stop(rightFrontThrust);
	}/*
	if (CommandInput == 5) {
		Shot();
	}*/
}

function Shot(){

	if (!shotting) {
		shotting = Instantiate(shot,objectPosRot.transform.position, objectPosRot.transform.rotation);	
	}	
}

function Play( thrust : GameObject) {

	var component : ParticleSystem;
	
	component = thrust.GetComponent(ParticleSystem);
	
	if (component.isPlaying == false){
		component.Play();
	}
}

function Stop( thrust : GameObject) {

	var component : ParticleSystem;
	
	component = thrust.GetComponent(ParticleSystem);
	component.Stop();	
	
}

@RPC
function SendMovementInput(CommandInput : int){	
	//Called on the server
	lastClientCommandInput = CommandInput;
}